/*
* 这些渲染方法不需要放到全局, 在需要的时候再加载
* */
import VXETable from "vxe-table";
import store from "./store";
import helper from "~/helper";

VXETable.formats.mixin({
    formatItemName({cellValue}) {
        if (!cellValue) return '-';
        if (isNaN(Number(cellValue))) return cellValue;
        let name = store.state.item.allName.hasOwnProperty(cellValue) ? store.state.item.allName[cellValue] : cellValue;
        return cellValue + ' ) ' + name;
    },
    formatGoodsModifyTypes1({cellValue}) {
        if (!cellValue) return '-';
        let lists = store.state.goodsModifyTypes.goodsModifyTypes1;
        let name = '';
        for (let i in lists) {
            if (lists[i].id === cellValue) {
                let data = lists[i];
                name = data.name;
                break;
            }
        }
        return name;
    },
    formatGoodsModifyTypes2({cellValue}) {
        if (!cellValue) return '-';
        let lists = store.state.goodsModifyTypes.goodsModifyTypes2;
        let name = '';
        for (let i in lists) {
            if (lists[i].id === cellValue) {
                let data = lists[i];
                name = data.name;
                break;
            }
        }
        return name;
    },
    formatGoodsModifyTypes3({cellValue}) {
        if (!cellValue) return '-';
        let lists = store.state.goodsModifyTypes.goodsModifyTypes3;
        let name = '';
        for (let i in lists) {
            if (lists[i].id === cellValue) {
                let data = lists[i];
                name = data.name;
                break;
            }
        }
        return name;
    },
    // 转化成int
    formatSeconds({cellValue}) {
        if (!cellValue) return '';
        return helper.formatSeconds(cellValue) || 0;
    },
});
/*
* 此处的代码需要配合promise语法, await, async语法使用, 否则会出现加载顺序不一致导致无法渲染的问题
*
import {itemAll, goodsModifyTypes1All, goodsModifyTypes2All, goodsModifyTypes3All} from '~/table-extends';
* ...
async function getData() {
    await itemAll();
    await goodsModifyTypes1All();
    await goodsModifyTypes2All();
    await goodsModifyTypes3All();
}
getData().then(() => {
    this.initData();
});
* */
export function itemAll() {
    if (!store.state.item.allName.length) return store.dispatch('item/All', 'item');
    return new Promise((resolve, reject) => {
        return resolve();
    });
}

export function goodsModifyTypes1All() {
    if (!store.state.goodsModifyTypes.goodsModifyTypes1.length) return store.dispatch('goodsModifyTypes/GetAll', 'goodsModifyTypes1');
    return new Promise((resolve, reject) => {
        return resolve();
    });
}

export function goodsModifyTypes2All() {
    if (!store.state.goodsModifyTypes.goodsModifyTypes2.length) return store.dispatch('goodsModifyTypes/GetAll', 'goodsModifyTypes2');
    return new Promise((resolve, reject) => {
        return resolve();
    });
}

export function goodsModifyTypes3All() {
    if (!store.state.goodsModifyTypes.goodsModifyTypes3.length) return store.dispatch('goodsModifyTypes/GetAll', 'goodsModifyTypes3');
    return new Promise((resolve, reject) => {
        return resolve();
    });
}
